var _prt = {};
_prt.ptList = [];
_prt.width = 210;
_prt.height = 297;
_prt.margin = [15,15,15,15];
_prt.lineHeight = 5;
_prt.preScale = 1;
_prt.preBgImg = null;
_prt.pageHead = null;
_prt.pageFoot = null;
_prt.add = function(v) {
  _prt.ptList.push(v);
}
_prt.clear = function() {
  _prt.ptList.length = 0;
}

_prt.setPageSize = function(w, h) {
  _prt.width = w;
  _prt.height = h;
}
_prt.setPreviewScale = function(sc) {
  _prt.preScale = sc;
}
_prt.setPreviewBgImg = function(img) {
  _prt.preBgImg = img;
}
_prt.setPageHead = function(str) {
  _prt.pageHead = str;
}
_prt.setPageFoot = function(str) {
  _prt.pageFoot = str;
}
_prt.setMargin = function (t, r, b, l) {
  _prt.margin[0] = t;
  _prt.margin[1] = r;
  _prt.margin[2] = b;
  _prt.margin[3] = l;
}
_prt.setLineHeight = function(h) {
  _prt.lineHeight = h*1.04;
}

_prt.preview = function() {
  var div = document.getElementById("div_preview");
  if(div) div.innerHTML = "";
  var prt = new ptview();
  prt.setViewSize(_prt.width, _prt.height);
  prt.setScale(_prt.preScale);
  prt.SetMargin(_prt.margin[0],_prt.margin[1],_prt.margin[2],_prt.margin[3]);
  if(_prt.pageHead) prt.SetPageHead(_prt.pageHead);
  if(_prt.pageFoot) prt.SetPageFoot(_prt.pageFoot);
  if(_prt.preBgImg) {
    var img = new Image();
    img.onload = function() {
      prt.setBgImg(img);
      prt.NewPage();
      _prt.setData(prt);
    };
    img.src = _prt.preBgImg;
    return;
  }
  prt.NewPage();
  _prt.setData(prt);
}


_prt.print = function() {
  /*if(!window.ActiveXObject) {
    window.alert("请使用IE浏览器并安装打印插件!");
    return false;
  }*/
  var prt = null;
  try {
    prt = new ActiveXObject("WillsPrint.WebPrint");
  } catch(e) {
    //window.alert("请安装打印插件并允许使用!");
    window.alert("请确认使用了IE浏览器安装了打印插件并允许使用!");
    return false;
  }
  if(_prt.ptList.length==0) {
    window.alert("没有可打印内容!");
    return false;
  }

  if(!prt.OpenPrinter()) return false;
  prt.SetMargin(_prt.margin[0],_prt.margin[1],_prt.margin[2],_prt.margin[3]);
  if(_prt.pageHead) prt.SetPageHead(_prt.pageHead);
  if(_prt.pageFoot) prt.SetPageFoot(_prt.pageFoot);
  _prt.setData(prt);
  if(typeof(printOk)=="function") printOk();
}

_prt.setData = function(prt) {
  prt.SetLineHeight(_prt.lineHeight);

  var i, v;
  for(i=0; i<_prt.ptList.length; i++) {
    v = _prt.ptList[i];
    if(v[0]=="pt") {
      prt.PrintText(v[1]);
    } else if(v[0]=="pl") {
      prt.PrintLine();
    } else if(v[0]=="pi") {
      prt.PrintImage(getFullUrl(v[1]),v[2],v[3]);
    } else if(v[0]=="nt") {
      prt.NextText(v[1],v[2]);
    } else if(v[0]=="dt") {
      prt.DrawText(v[1],v[2],v[3]);
    } else if(v[0]=="dl") {
      prt.DrawLine(v[1],v[2],v[3],v[4]);
    } else if(v[0]=="di") {
      prt.DrawImage(getFullUrl(v[1]),v[2],v[3],v[4],v[5]);
    } else if(v[0]=="sf") {
      prt.SetFont(v[1]);
    } else if(v[0]=="sp") {
      prt.SetPos(v[1],v[2]);
    } else if(v[0]=="mx") {
      prt.MoveX(v[1]);
    } else if(v[0]=="my") {
      prt.MoveY(v[1]);
    } else if(v[0]=="np") {
      prt.NewPage();
    } else if(v[0]=="nl") {
      prt.NewLine(v[1]);
    }

  }

  prt.PrintEnd();
  return true;
}

function getFullUrl(url) {
  if(url.substr(0,7)=="http://" || url.substr(0,8)=="https://")
    return url;
  var hurl = document.location.href;
  if(url.substr(0,1)=="/")
    return hurl.substr(0,hurl.indexOf("/",10))+url;
  else
    return hurl.substr(0,hurl.lastIndexOf("/")+1)+url;
}

var ptview = function() {
  var width = 210;
  var height = 297;
  var marginLeft = 15;
  var marginTop = 15;
  var marginRight = 15;
  var marginBottom = 15;
  var lineHeight = 5;
  var bgImg = null;
  var pageHead = null;
  var pageFoot = null;
  var font = "9pt 宋体";
  var scale = 1;
  var bReady = false;
  var curX = 0;
  var curY = 0;
  var pageNo = 0;
  var ctx = null;

  var getRealLen = function(n) {
    return parseInt(n*3.7);
  }
  var getPrtLen = function(n) {
    return parseInt(n*100/3.7)/100;
  }

  this.setViewSize = function(w,h) {
    width = w;
    height = h;
  }
  this.setScale = function(sc) {
    scale = sc;
  }
  this.setBgImg = function(img) {
    bgImg = img;
  }
  this.SetPageHead = function(str) {
    pageHead = str;
  }
  this.SetPageFoot = function(str) {
    pageFoot = str;
  }
  this.SetMargin = function (t, r, b, l) {
    marginTop = t;
    marginRight = r;
    marginBottom = b;
    marginLeft = l;
  }
  this.SetLineHeight = function(h) {
    lineHeight = h;
  }

  var IsReady = function() {
    if(ctx==null) return false;
    if(curX==0) curX = marginLeft;
    if(curY==0) curY = marginTop;
    return true;
  }

  this.NewPage = function() {
    return NextPage();
  }
  this.NewLine = function(l) {
    if(!IsReady()) return false;
    NextLine(l);
    return true;
  }
  this.PrintEnd = function() {
    return true;
  }

  var NextPage = function() {
    var div = document.getElementById("div_preview");
    if(!div) return false;
    pageNo++;
    var div1 = document.createElement("DIV");
    div1.style.cssText = "margin:10px;";
    div.appendChild(div1);
    div1.innerHTML = '<canvas id="cavpg_'+pageNo+'" width="'+getRealLen(width)+'" height="'+getRealLen(height)+'" style="position:relative; background-color:#fff; width:'+getRealLen(width*scale)+'px; height:'+getRealLen(height*scale)+'px;"></canvas>';
    var canvas = document.getElementById("cavpg_"+pageNo);
    if(!canvas.getContext && window.G_vmlCanvasManager)
      window.G_vmlCanvasManager.initElement(canvas);
    if(!canvas.getContext) return false;
    ctx = canvas.getContext('2d');
    ctx.font = "9pt 宋体";
    ctx.textBaseline = "top";
    ctx.strokeStyle = "#000";
    ctx.lineWidth = 1;

    if(pageHead) {
      curX = marginLeft;
      curY = marginTop - lineHeight*1.2;
      ctx.fillText(pageHead.replace("{pg}", pageNo), getRealLen(curX), getRealLen(curY));
      curY += lineHeight*0.6;
      ctx.beginPath();
      ctx.moveTo(getRealLen(curX)-0.5, getRealLen(curY+lineHeight*0.3)+0.5);
      ctx.lineTo(getRealLen(width-marginRight)+0.5, getRealLen(curY+lineHeight*0.3)+0.5);
      ctx.stroke();
    }
    if(pageFoot) {
      curX = marginLeft;
      curY = height-marginBottom - lineHeight*0;
      ctx.beginPath();
      ctx.moveTo(getRealLen(curX)-0.5, getRealLen(curY+lineHeight*0.3)+0.5);
      ctx.lineTo(getRealLen(width-marginRight)+0.5, getRealLen(curY+lineHeight*0.3)+0.5);
      ctx.stroke();
      curY += lineHeight*0.6;
      ctx.fillText(pageFoot.replace("{pg}", pageNo), getRealLen(curX), getRealLen(curY));
    }

    ctx.font = font;
    curX = marginLeft;
    curY = marginTop;

    /*ctx.beginPath();
     ctx.moveTo(getRealLen(curX), getRealLen(curY));
     ctx.lineTo(getRealLen(width-marginRight), getRealLen(curY));
     ctx.lineTo(getRealLen(width-marginRight), getRealLen(height-marginBottom));
     ctx.lineTo(getRealLen(curX), getRealLen(height-marginBottom));
     ctx.closePath();
     ctx.stroke();*/

    if(bgImg) ctx.drawImage(bgImg, 0, 0, getRealLen(width), getRealLen(height));

    bReady = true;
    return true;
  }
  var NextLine = function(l) {
    curX = marginLeft;
    curY += lineHeight*l;
    if(curY+lineHeight>height-marginBottom)
      NextPage();
  }

  this.SetPos = function(x, y) {
    if(!IsReady()) return false;
    curX = x;
    curY = y;
  }
  this.MoveX = function(x) {
    if(!IsReady()) return false;
    curX += x;
  }
  this.MoveY = function(y) {
    if(!IsReady()) return false;
    curY += y;
  }
  this.SetFont = function(f) {
    if(!IsReady()) return false;
    ctx.font = f;
    font = f;
  }

  this.PrintText = function(str) {
    if(!IsReady()) return false;
    var n1, n2, nn, nl;

    if(str=="") {
      NextLine(0.5);
    } else {
      n2 = 0;
      nn = str.length;
      nl = width - marginLeft - marginRight;
      while(n2<=nn) {
        n1 = n2;
        while(n2<=nn) {
          n2 = n2 + 1;
          if(getPrtLen(ctx.measureText(str.substr(n1, n2 - n1 + 1)).width)>nl) break;
        }
        ctx.fillText(str.substr(n1, n2 - n1), getRealLen(curX), getRealLen(curY));
        NextLine(1);
      }
    }
  }
  this.PrintLine = function() {
    if(!IsReady()) return false;
    ctx.beginPath();
    ctx.moveTo(getRealLen(curX)-0.5, getRealLen(curY+lineHeight*0.3)+0.5);
    ctx.lineTo(getRealLen(width-marginRight)+0.5, getRealLen(curY+lineHeight*0.3)+0.5);
    ctx.stroke();
    NextLine(0.8);
  }
  this.PrintImage = function(url, w, h) {
    if(!IsReady()) return false;
    var x = curX;
    var y = curY;
    var c = ctx;
    var img = new Image();
    img.onload = function() {
      c.drawImage(img, getRealLen(x), getRealLen(y), getRealLen(w), getRealLen(h));
    }
    img.src = url;
    curY += h;
    NextLine(0.5);
  }
  this.NextText = function(str, t) {
    if(!IsReady()) return false;
    var x, nl, n, nn;
    x = curX;
    nl = width - marginLeft - marginRight;
    if(x>marginLeft) {
      if(t>0 && t<1) {
        nn = t * nl
        n = parseInt((x - marginLeft + nn - 1) / nn);
        curX = n * nn + marginLeft;
        if(curX+nn/3 > nl+marginLeft) NextLine(1);
      }
    }
    var l = getPrtLen(ctx.measureText(str).width);
    ctx.fillText(str, getRealLen(curX), getRealLen(curY));
    curX += l;
  }
  this.DrawText = function(str, x, y) {
    if(!IsReady()) return false;
    var l = getPrtLen(ctx.measureText(str).width);
    ctx.fillText(str, getRealLen(x), getRealLen(y));
    curX = x+l;
    curY = y;
  }
  this.DrawLine = function(x, y, x2, y2) {
    if(!IsReady()) return false;
    ctx.beginPath();
    ctx.moveTo(getRealLen(x)+0.5, getRealLen(y)+0.5);
    ctx.lineTo(getRealLen(x2)+0.5, getRealLen(y2)+0.5);
    ctx.stroke();
  }
  this.DrawImage = function(url, x, y, w, h) {
    if(!IsReady()) return false;
    var c = ctx;
    var img = new Image();
    img.onload = function() {
      c.drawImage(img, getRealLen(x), getRealLen(y), getRealLen(w), getRealLen(h));
    }
    img.src = url;
  }

}



function resetHeight() {
  var hh = 0;
  if(document.documentElement.clientHeight>0 && (hh==0 || document.documentElement.clientHeight<hh)) hh = document.documentElement.clientHeight;
  if(document.body.clientHeight>0 && (hh==0 || document.body.clientHeight<hh)) hh = document.body.clientHeight;
  var div = document.getElementById("div_preview");
  if(div) div.style.height = (hh-98)+"px";
}

function setHeight() {
  if(window.attachEvent)
    window.attachEvent("onresize", resetHeight);
  else if(window.addEventListener)
    window.addEventListener("resize", resetHeight, false);
  resetHeight();
}
